import React from "react";
import ReactDOM from "react-dom";
import {HashRouter as Router, Route, Redirect, Switch} from "react-router-dom"
import {Provider} from "mobx-react";
import allStore from "./stores";
import {asyncComponent} from "react-async-component";
import {Tools} from "weaver-mobile";

import "./style";

// const getLabel = Tools.getLabel;
const getLocaleLabel = Tools.getLocaleLabel.bind(this, "prj_mobile");

const coms = {
    Main: asyncComponent({
        resolve: () => import("./components/main")
    }),
    Index: asyncComponent({
        resolve: () => import("./components/main/enter")
    }),
    ProjectDetails: asyncComponent({
        resolve: () => import("./components/projects/projectDetails")
    }),
    TaskDetails: asyncComponent({
        resolve: () => import("./components/task/taskDetails")
    }),
    TaskForm: asyncComponent({
        resolve: () => import("./components/task/taskForm")
    }),
    FormOperations: asyncComponent({
        resolve: () => import("./components/form/formOperations")
    }),
    ReplyEditor: asyncComponent({
        resolve: () => import("./components/reply/replyEditor")
    }),
    MyProjectPage: asyncComponent({
        resolve: () => import("./components/singlePage/myProjectPage")
    }),
};


class Root extends React.Component {
    /*    constructor(props) {
            super(props);
            this.state = {
                loadLang: false
            };
        }*/

    /*    componentDidMount() {
            Tools.ssoInit().then(() => {
                getLocaleLabel(() => {
                    this.setState({loadLang: true});
                });
            }).catch((res) => {
                let reason = "";
                if (JSON.stringify(res) !== "{}") {
                    reason = "," + res;
                }
                Bubble.error(getLabel("", "登录失败") + reason, 0);
            });
        }*/

    render() {
        return (
            <Provider {...allStore}>
                <Router>
                    <Route name="main" path="/" onEnter={getLocaleLabel} render={() => (
                        <coms.Main>
                            <Switch>
                                <Route name="proj" path="/index/:tab" component={coms.Index}/>
                                <Route name="ProjectDetails" path="/ProjectDetails/:index/:prjid"
                                       component={coms.ProjectDetails}/>
                                <Route name="TaskDetails" path="/TaskDetails/:index/:taskid"
                                       component={coms.TaskDetails}/>
                                <Route name="TaskForm" path="/TaskForm/:taskid/:viewtype" component={coms.TaskForm}/>
                                <Route name="FormOperations" path="/FormOperations" component={coms.FormOperations}/>
                                <Route name="ReplyEditor" path="/ReplyEditor" component={coms.ReplyEditor}/>
                                <Route name="MyProjectPage" path="/myprojectsingle" component={coms.MyProjectPage}/>
                                <Redirect exact path='/' to={{pathname: "/index/home"}}/>
                            </Switch>
                        </coms.Main>
                    )}/>
                </Router>
            </Provider>
        )
    }
}

// ReactDOM.render(<Root/>, document.getElementById("root"));
Tools.getLocaleLabel("prj_mobile", () => {
    ReactDOM.render(<Root/>, document.getElementById("root"))
})
